<?php


namespace app\home\controller;


use app\base\BaseController;
use think\facade\Db;
use think\facade\View;

class KcCg extends BaseController
{
    protected $title='库存采购表';
    protected $table='sale_contract_detail';
    private $sessionKey='pageKcCg';
    public function index()
    {
        $param=$this->param;
        if (request()->isAjax()) {

            $where[]=['scd.status','=',4];
            $where[]=['scd.type','=',2];
//            foreach (['bar_code','mdName','specs','brand'] as $i){
//                if (isset($param[$i])&&$param[$i]){
//                    $where[]=[$i,'like',"%".$param[$i].'%'];
//                }
//            }
            if (isset($param['brand'])&&$param['brand']){
                $where[]=['md.brand','like',"%".$param['brand'].'%'];
            }
            if (isset($param['mdName'])&&$param['mdName']){
                $where[]=['md.name','like',"%".$param['mdName'].'%'];
            }
            if (isset($param['specs'])&&$param['specs']){
                $where[]=['md.specs','like',"%".$param['specs'].'%'];
            }
            if (isset($param['buyCode'])&&$param['buyCode']){
                $where[]=['md.buyCode','like',"%".$param['buyCode'].'%'];
            }
            if (isset($param['taxCode'])&&$param['taxCode']){
                $where[]=['md.taxCode','like',"%".$param['taxCode'].'%'];
            }
            if (isset($param['cgrkd'])&&$param['cgrkd']){
                $cgMxId=Db::name($this->table)
                    ->where('type',3)
                    ->where('status',2)
                    ->where('stokcOrderNum','like','%'.$param['cgrkd'].'%')
                    ->column('saleContractDetailId');
                $xsmxId=Db::name('sale_contract_detail')
                    ->where('status',4)
                    ->where('type',2)
                    ->where('id','in',$cgMxId)
                    ->column('saleContractDetailId');
                $where[]=['saleContractDetailId','in',$xsmxId];
            }
//            halt($where);
            if (isset($param['cght'])&&$param['cght']){
                $where[]=['sc.orderNum','like',"%".$param['cght'].'%'];
            }
            if (isset($param['xsHt'])&&$param['xsHt']){
                $where[]=['scd.saleContractOrderNum','like',"%".$param['xsHt'].'%'];
            }

            if (isset($param['pName'])&&$param['pName']){
                $where[]=['p.name','like',"%".$param['pName'].'%'];
            }
            session($this->sessionKey,$where);
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name($this->table)
                ->alias(' scd')
                ->leftJoin('sale_contract sc','sc.id = scd.orderId')
                ->leftJoin('material_dossier md','md.id=scd.mdId')
                ->leftJoin('purveyor p','p.id=sc.punterId')
                ->where($where)

                ->field("scd.*,sc.orderNum as orderNum,
                md.name as mdName, md.brand,md.specs,md.buyCode,md.taxCode,md.unit,p.name as pName")
                ->paginate($rows)->each(function ($item,$index){
                    $lockNum=Db::name('sale_contract_lock')
                        ->where('status',1)
                        ->where('detailId',$item['saleContractDetailId'])
                        ->sum('lockNum');
                    $kcNum=Db::name('stock_order_detail')
                        ->where('status',2)
                        ->where('type',3)
                        ->where('saleContractDetailId',$item['id'])
                        ->sum('num');
                    $item['lockNum']=$lockNum;
                    $item['kcNum']=$kcNum;
                    $canUse=$item['bjNum']-$lockNum;
                    $kcMoney=$kcNum*$item['bjPrice'];
                    $item['canUse']=$canUse;
                    $item['kcMoney']=round($kcMoney,2);

                    return $item;
                });
            return table_assign(0, '', $list);
        } else {
            $warehouse=Db::name('warehouse')->where('status',1)->field('id,name')->select();
            View::assign('warehouseList',$warehouse);
            return view();
        }

    }
}